import BScroll from "@better-scroll/core";
import ObserveDOM from "@better-scroll/observe-dom";
import { onMounted, onUnmounted, ref } from "@vue/runtime-core";

BScroll.use(ObserveDOM) // 自动探测DOM更新

export default function useScroll(wrapper, options, emit) {
    const scroll = ref(null)
    onMounted(() => {
        scroll.value = new BScroll(wrapper.value, {ObserveDOM: true, ...options});
        if (options.probeType > 0) {
            scroll.value.on('scroll', (pos) => {
                emit('scroll', pos)
            })
        }
    })

    onUnmounted(() => {
        scroll.value.destroy()
    })

    return scroll
}